//
//  ModelView.m
//  YIJU2.0-HD
//
//  Created by 张金国 on 16/5/9.
//  Copyright © 2016年 张金国. All rights reserved.
//

#import "ModelView.h"
#import "Masonry.h"


#import "SelectScreenView.h"
#import "ScreenMoveView.h"
#import "ModelLampControlView.h"
#import "HouseInfoControlView.h"

#import "Macro.h"
#import "TitleView.h"
#import "NKColorSwitch.h"
#import "UIColor+RandomColor.h"
#import "UIButton+ColorandText.h"
#import "SearchView.h"

@interface ModelView ()<ScreenMoveViewDelegate>

/**
 *  请选择显示器
 */
@property (nonatomic ,strong)SelectScreenView  *selectScreenView;
/**
 *  灯光颜色控制
 */
@property (nonatomic ,strong)UIView  *lampColorView;
/**
 *  显示器移动控制
 */
@property (nonatomic ,strong)ScreenMoveView  *screenMoveView;
/**
 * 搜索房源并控制
 */
@property (nonatomic ,strong)UIView  *houseInfocontrolView;
/**
 *  模型灯光控制
 */
@property (nonatomic ,strong)ModelLampControlView  *modelLampcontrolView;



@end

@implementation ModelView
#pragma mark - init
- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupConstraints];
        
    }
    return self;
}
#pragma mark - private
- (void)setupConstraints {
     [self.selectScreenView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.left.right.equalTo(self);
     make.height.equalTo(self.mas_height).multipliedBy(0.2);
     }];
     [self.lampColorView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(self.selectScreenView.mas_bottom);
     make.left.equalTo(self.mas_left);
     make.width.equalTo(self.mas_width).multipliedBy(0.5);
     make.height.equalTo(self.mas_height).multipliedBy(0.2);
     }];
     [self.screenMoveView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(self.lampColorView.mas_top);
     make.left.equalTo(self.lampColorView.mas_right);
     make.right.equalTo(self.mas_right);
     make.height.equalTo(self.lampColorView.mas_height);
     }];
     
     [self.houseInfocontrolView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(self.lampColorView.mas_bottom);
     make.left.right.equalTo(self);
     make.height.equalTo(self.mas_height).multipliedBy(0.37);
     }];
     [self.modelLampcontrolView mas_makeConstraints:^(MASConstraintMaker *make) {
     make.top.equalTo(self.houseInfocontrolView.mas_bottom);
     make.left.right.bottom.equalTo(self);
     }];
     

    [self layoutlampColorView];
    [self layouthouseInfocontrolView];
    
}

/**
 *  每个小view里子控件布局
 */
- (void)layoutlampColorView{
    TitleView *titleView2 = [[TitleView alloc ] initWithLineColor:[UIColor redColor] title:@"灯光颜色控制" titleFont:kLabelTextColor];
    [self.lampColorView addSubview:titleView2];
    [titleView2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.lampColorView).offset(10);
        make.height.equalTo(self.selectScreenView.mas_height).multipliedBy(1/4.0f);
        make.width.equalTo(self.selectScreenView.mas_width).multipliedBy(1/4.0f);
    }];
    NKColorSwitch *nkColorSwitch = nil;
    UILabel *templabel = nil;
    for (int i = 0; i < 3; i ++) {
        NKColorSwitch *nkColorSwitch1 = [[NKColorSwitch alloc]initWithFrame:CGRectMake(20, 90, SCREEN_WIDTH/12, SCREEN_HEIGHT/30)];
        [nkColorSwitch1 addTarget:self action:@selector(switchPressed:) forControlEvents:UIControlEventValueChanged];
        [nkColorSwitch1 setTintColor:[UIColor randomColor]];
        [nkColorSwitch1 setOnTintColor:[UIColor randomColor]];
        [nkColorSwitch1 setThumbTintColor:[UIColor whiteColor]];
        [self.lampColorView addSubview:nkColorSwitch1];
        UILabel *label = [[UILabel alloc]init];
        label.text = @"灯光控制";
        [self.lampColorView addSubview:label];
        
        if (nkColorSwitch == nil) {
            [nkColorSwitch1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleView2.mas_bottom).offset(30);
                make.left.equalTo(self.lampColorView.mas_left).offset(20);
                make.width.equalTo(self.lampColorView.mas_width).multipliedBy(1/5.0f);
                make.height.equalTo(self.lampColorView.mas_height).multipliedBy(1/3.0f);
            }];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(nkColorSwitch1.mas_bottom).offset(10);
                make.left.equalTo(nkColorSwitch1.mas_left);
            }];
        }else {
            [nkColorSwitch1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(titleView2.mas_bottom).offset(30);
                make.left.equalTo(nkColorSwitch.mas_right).offset(20);
                make.width.equalTo(self.lampColorView.mas_width).multipliedBy(1/5.0f);
                make.height.equalTo(self.lampColorView.mas_height).multipliedBy(1/3.0f);
            }];
            [label mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(nkColorSwitch1.mas_bottom).offset(10);
                make.left.equalTo(nkColorSwitch1.mas_left);
                
            }];
            
        }
        nkColorSwitch = nkColorSwitch1;
        templabel = label;
        if (i == 2) {
            UIButton *button = [UIButton buttonWithBackgroundColor:[UIColor randomColor] textFont:kLabelTextColor textColor:[UIColor whiteColor] title:@"灯光全关" cornerRadius:5.0f];
            [self.modelLampcontrolView  addSubview:button];
            [button mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(nkColorSwitch.mas_top);
                make.left.equalTo(nkColorSwitch.mas_right).offset(20);
                make.width.equalTo(self.lampColorView.mas_width).multipliedBy(1/10.0f*2);
                make.height.equalTo(self.lampColorView.mas_height).multipliedBy(1/5.0f);
            }];
        }
        
    }
}
- (void)layouthouseInfocontrolView {
    TitleView *titleView4 = [[TitleView alloc]initWithLineColor:[UIColor redColor] title:@"搜索房源并控制" titleFont:kLabelTextColor];
    [self.houseInfocontrolView addSubview:titleView4];
    [titleView4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self.houseInfocontrolView).offset(10);
        make.height.equalTo(self.selectScreenView.mas_height).multipliedBy(1/4.0f);
        make.width.equalTo(self.selectScreenView.mas_width).multipliedBy(1/4.0f);
    }];
    UILabel *subtitle = [[UILabel alloc]init];
    subtitle.text = @"精确房源搜索";
    [self.houseInfocontrolView addSubview:subtitle];
    [subtitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(titleView4.mas_bottom);
        make.left.equalTo(titleView4.mas_left);
    }];
    
    SearchView *tempsearchView = nil;
    
    for (int i = 0 ; i < 4 ; i ++) {
        SearchView *searchView = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
        [self.houseInfocontrolView addSubview:searchView];
        
        if (tempsearchView == nil) {
            [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(subtitle.mas_bottom).offset(10);
                make.left.equalTo(subtitle.mas_left);
                make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
            }];
        }else {
            [searchView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(subtitle.mas_bottom).offset(10);
                make.left.equalTo(tempsearchView.mas_right).offset(20);
                make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
            }];
            
        }
        tempsearchView = searchView;
        
        if (i == 0) {
            UILabel *subtitle1 = [[UILabel alloc]init];
            subtitle1.text = @" 按条件搜索(可多选)";
            [self.houseInfocontrolView addSubview:subtitle1];
            [subtitle1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(searchView.mas_bottom).offset(10);
                make.left.equalTo(searchView.mas_left);
            }];
            
            
            SearchView *tempsearchView2 = nil;
            for (int j = 0; j < 4; j++) {
                SearchView *searchView2 = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
                [self.houseInfocontrolView addSubview:searchView2];
                if (tempsearchView2 == nil) {
                    [searchView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(subtitle1.mas_bottom).offset(10);
                        make.left.equalTo(subtitle1.mas_left);
                        make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                        make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
                    }];
                }else {
                    [searchView2 mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.top.equalTo(subtitle1.mas_bottom).offset(10);
                        make.left.equalTo(tempsearchView2.mas_right).offset(20);
                        make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                        make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
                    }];
                }
                tempsearchView2 = searchView2;
                
                if (j == 0) {
                    SearchView *tempsearchView3 = nil;
                    for (int k = 0 ; k < 4; k ++) {
                        SearchView *searchView3 = [[SearchView alloc]initWithTitle:@"栋号" titleFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] cornerRadius:5.0f backgroundColor:[UIColor whiteColor]];
                        [self.houseInfocontrolView addSubview:searchView3];
                        if (tempsearchView3 == nil) {
                            [searchView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.top.equalTo(searchView2.mas_bottom).offset(20);
                                make.left.equalTo(searchView2.mas_left);
                                make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                                make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
                            }];
                        }else {
                            [searchView3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.top.equalTo(tempsearchView3.mas_top);
                                make.left.equalTo(tempsearchView3.mas_right).offset(20);
                                make.width.equalTo(self.houseInfocontrolView.mas_width).multipliedBy(1/5.40f);
                                make.height.equalTo(self.houseInfocontrolView.mas_height).multipliedBy(1/8.0f);
                            }];
                        }
                        tempsearchView3 = searchView3;
                        if (k == 3) {
                            
                            UIButton *button3 = [UIButton buttonWithBackgroundColor:[UIColor randomColor] textFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] textColor:[UIColor whiteColor] title:@"条件搜索" cornerRadius:5.0f];
                            button3.titleLabel.textColor = [UIColor blackColor];
                            [self.houseInfocontrolView  addSubview:button3];
                            [button3 mas_makeConstraints:^(MASConstraintMaker *make) {
                                make.top.equalTo(searchView3.mas_top);
                                make.left.equalTo(searchView3.mas_right).offset(35);
                                make.width.equalTo(self.modelLampcontrolView.mas_width).multipliedBy(1/10.0f);
                                make.height.equalTo(searchView3.mas_height);
                            }];
                        }
                    }
                }
            }
        }
        if (i == 3) {
            UIButton *button1 = [UIButton buttonWithBackgroundColor:[UIColor randomColor] textFont:[UIFont preferredFontForTextStyle:UIFontTextStyleTitle3] textColor:[UIColor whiteColor] title:@"房号搜索" cornerRadius:5.0f];
            [self.houseInfocontrolView  addSubview:button1];
            [button1 mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(searchView.mas_top);
                make.left.equalTo(searchView.mas_right).offset(35);
                make.width.equalTo(self.modelLampcontrolView.mas_width).multipliedBy(1/10.0f);
                make.height.equalTo(searchView.mas_height);
            }];
        }
    }
}

#pragma mark - ScreenMoveViewDelegate
- (void)screenMoveViewSelectIndex:(SelectType)selectType {
    DDLogInfo(@"selectType%ld",selectType);
    
}

#pragma mark - Responde Event
- (void)handleButtonEvent:(UIButton *)button {
    
    DDLogInfo(@"qwe");
    
}
- (void)switchPressed:(id)sender{
    NKColorSwitch *nkswitch = (NKColorSwitch *)sender;
    if (nkswitch.isOn)
        DDLogInfo(@"switchPressed ON");
    else
        DDLogInfo(@"switchPressed OFF");
}


#pragma mark - getter
- (SelectScreenView *)selectScreenView{
    if (!_selectScreenView) {
        _selectScreenView = [[SelectScreenView alloc]initWithHandleButtonEventBlock:^(SelcetScreenType selcetScreenType) {
            DDLogInfo(@"selcetScreenType%ld",selcetScreenType);
        }];
        [self addSubview:_selectScreenView];
    }
    return _selectScreenView;
}
- (UIView *)lampColorView{
    if (!_lampColorView) {
        _lampColorView = [[UIView alloc]init];
        [self addSubview:_lampColorView];
    }
    return _lampColorView;
}
- (ScreenMoveView *)screenMoveView{
    if (!_screenMoveView) {
        _screenMoveView = [[ScreenMoveView alloc]init];
        _screenMoveView.delegate  = self;
        [self addSubview:_screenMoveView];
    }
    return _screenMoveView;
}
- (UIView *)houseInfocontrolView{
    if (!_houseInfocontrolView) {
        _houseInfocontrolView = [[UIView alloc]init];
        [self addSubview:_houseInfocontrolView];
    }
    return _houseInfocontrolView;
}
- (ModelLampControlView *)modelLampcontrolView{
    if (!_modelLampcontrolView) {
        _modelLampcontrolView = [[ModelLampControlView alloc]initWithModelLampControlViewBlock:^(ModelLampControlButtonType modelLampControlButtonType) {
            DDLogInfo(@"modelLampControlButtonType%ld",modelLampControlButtonType);
        }];
        [self addSubview:_modelLampcontrolView];
    }
    return _modelLampcontrolView;
}




@end
